package cn.yingtian.image;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.util.Log;

public class NetWorkTools {
	public static byte[] getData(String url) {
		InputStream is = null;
		URL myurl;
		HttpURLConnection conn = null;
		try {
			myurl = new URL(url);
			conn = (HttpURLConnection) myurl.openConnection();
			conn.setRequestMethod("GET");
			is = conn.getInputStream();
			System.out.println(url+":------:"+is.available());
			ByteArrayOutputStream baos=new ByteArrayOutputStream();
			byte[] buf = new byte[1024];
			int length=0;
			while((length=is.read(buf))!=-1){
				baos.write(buf, 0, length);
			}
			buf=baos.toByteArray();
			baos.close();
			System.out.println(url+":------:buf length"+buf.length);
			return buf;
		} catch (IOException e) {
			Log.e("MainActivity", e.getMessage());
			return null;
		} finally {
			if (conn != null) {
				conn.disconnect();
			}
		}
	}

	public static InputStream getInputStream(String url) {
		URL myurl;
		HttpURLConnection conn = null;
		try {
			myurl = new URL(url);
			conn = (HttpURLConnection) myurl.openConnection();
			conn.setRequestMethod("GET");
			return conn.getInputStream();
		} catch (MalformedURLException e) {
			Log.e("MainActivity", e.getMessage());
			return null;
		} catch (IOException e) {
			Log.e("MainActivity", e.getMessage());
			return null;
		}
	}
}
